﻿/*++
 * Copyright (c) DemeterSoft Corporation
 *
 * File Name:
 *     NavigationPanel.cs
 *     
 * Abstract:
 *     导航面板，主要用于导航
 *
 * Revision History:
 *     08/29/2011    yliu
 *         创建文件
 * --*/
using System;
using System.Collections.Generic;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using SmartSpace.UI.Silverlight;
using SmartSpace.UI.Silverlight.Theme;

namespace Themes.Default
{
    /// <summary>
    /// 导航面板
    /// </summary>
    public partial class NavigatePanel : SlView, INavigatePanel
    {
        #region Variables

        /// <summary>
        /// 导航委托事件
        /// </summary>
        public event Action<bool> NavigationPanelCollapsed;

        public event NavigatingEventHandler Navigating;

        #endregion

        #region Constructors

        public NavigatePanel()
        {
            InitializeComponent();          
        }

        #endregion

        #region INavigatePanel Members

        /// <summary>
        /// 载入用户具有权限的导航
        /// </summary>
        /// <param name="navigations"></param>
        /// <param name="shortCuts"></param>
        public void LoadNavigations(IEnumerable<SlNavigation> navigations, IEnumerable<SlShortcut> shortcuts)
        {
            List<SlNavigation> links = new List<SlNavigation>();
            foreach (SlNavigation model in navigations)
            {
                links.Add(model);
            }

            m_accordionNavigations.ItemsSource = links;

            this.OnButtonExpandedClick(null, null);
        }

        /// <summary>
        /// 选中手风琴效果控件记录触发的事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void OnAccordionItemSelected(object sender, RoutedEventArgs e)
        {
            AccordionItem accordionItem = sender as AccordionItem;
            SlNavigation model = accordionItem.Tag as SlNavigation;

            #region set header

            StackPanel panel = new StackPanel();
            panel.Orientation = Orientation.Horizontal;

            if (!string.IsNullOrEmpty(model.IconPath))
            {
                Image image = new Image();
                image.Margin = new Thickness(5, 3, 0, 3);
                image.Stretch = Stretch.Fill;

                image.Source = new BitmapImage(new Uri(model.IconPath, UriKind.Relative));

                panel.Children.Add(image);
            }

            TextBlock text = new TextBlock();
            text.Text = model.Name;
            text.FontSize = 15;
            text.Margin = new Thickness(5, 0, 0, 0);
            text.VerticalAlignment = System.Windows.VerticalAlignment.Center;
            panel.Children.Add(text);

            this.m_headerTitle.Content = panel;

            #endregion

            //m_treeNaigations.ItemsSource = model.Children;
            //if (model.Children.Count > 0)
            //{
            //    m_treeNaigations.SelectedItem = model.Children[0];
            //}
        }

        public SlView GetContentView()
        {
            return this;
        }

        #endregion

        /// <summary>
        /// 选中模块中导航触发的事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void OnTreeNaigationsSelectedItemChanged(object sender, RoutedPropertyChangedEventArgs<object> e)
        {
            SlNavigation navigation = e.NewValue as SlNavigation;
            if (navigation != null)
            {
                Navigating(this, new NavigatingEventArgs(navigation.Name, navigation.View));
            }
        }

        /// <summary>
        /// 收缩按钮事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public void OnButtonCollapseClick(object sender, RoutedEventArgs e)
        {
            //AccordionItem accordionItem = m_accordionNavigations.SelectedItem as AccordionItem;
            SlNavigation model = m_accordionNavigations.SelectedItem as SlNavigation;
            borderLabel.Tag = model.Name;
            VisualStateManager.GoToState(borderLabel, "Collapsed", false);
            if (NavigationPanelCollapsed != null)
            {
                NavigationPanelCollapsed(true);
            }
        }

        /// <summary>
        /// 展开按钮事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public void OnButtonExpandedClick(object sender, RoutedEventArgs e)
        {
            VisualStateManager.GoToState(borderLabel, "Expanded", false);
            if (NavigationPanelCollapsed != null)
            {
                NavigationPanelCollapsed(false);
            }
        }

        /// <summary>
        /// 鼠标离开Border收缩列表
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void OnBorderMouseLeave(object sender, System.Windows.Input.MouseEventArgs e)
        {
            OnButtonCollapseClick(null, null);
        }

        /// <summary>
        /// 鼠标移动至Border上展开列表
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void OnBorderMouseEnter(object sender, System.Windows.Input.MouseEventArgs e)
        {
            OnButtonExpandedClick(null, null);
        }

        public void LostNavigationFocus()
        {
            //m_treeNaigations.ClearSelection();
        }

        private void m_accordionNavigations_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (e.AddedItems.Count == 1)
            {
                if (e.RemovedItems.Count == 0)
                {
                    return;
                }
                else
                {
                    SlNavigation selectedItem = e.AddedItems[0] as SlNavigation;
                    Accordion accordion = sender as Accordion;

                    if (selectedItem != null)
                    {
                        SlNavigation subItem = CanActivedFirstSubItem(selectedItem);


                        AccordionItem accordionItem = (AccordionItem)accordion.ItemContainerGenerator.ContainerFromItem(selectedItem);
                        TreeViewItem treeViewItem = FindVisualChild(accordionItem, subItem);
                        //TreeView treeView = accordionItem.ContentTemplate.LoadContent() as TreeView;
                        //TreeViewItem treeViewItem = (TreeViewItem)treeView.GetContainerFromItem(subItem);
                        if (treeViewItem != null)
                        {
                            treeViewItem.IsSelected = false;
                            treeViewItem.IsSelected = true;
                        }
                    }
                }
            }
        }

        private SlNavigation CanActivedFirstSubItem(SlNavigation parent)
        {
            foreach (SlNavigation item in parent.Children)
            {
                if (item.Children!=null && item.Children.Count>0)
                {
                    return CanActivedFirstSubItem(item);
                }
                else
                {
                    return item;
                }
            }
            return null;
        }

        private TreeViewItem FindVisualChild(DependencyObject obj, object dataContext)
        {
            for (int i = 0; i < VisualTreeHelper.GetChildrenCount(obj); i++)
            {
                DependencyObject child = VisualTreeHelper.GetChild(obj, i);
                if (child != null && child is TreeViewItem && (child as TreeViewItem).DataContext == dataContext)
                {
                    return (TreeViewItem)child;
                }
                else
                {
                    TreeViewItem childOfChild = FindVisualChild(child, dataContext);
                    if (childOfChild != null)
                    {
                        return childOfChild;
                    }
                }
            }

            return null;
        }

    }
}
